<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :default-expanded-keys="[1]"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="机井编号" prop="wellNumber">
        <el-input
          v-model="queryParams.wellNumber"
          placeholder="请输入机井编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原机井编号" prop="oldWellNumber">
        <el-input
          v-model="queryParams.oldWellNumber"
          placeholder="请输入原机井编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="乡镇" prop="town">
        <el-input
          v-model="queryParams.town"
          placeholder="请输入乡镇"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机井建设年份" prop="constructionYear">
        <el-input
          v-model="queryParams.constructionYear"
          placeholder="请输入机井建设年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['farmwell:well:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['farmwell:well:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['farmwell:well:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['farmwell:well:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wellList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机井编号" align="center" prop="wellNumber" />
      <el-table-column label="立项年份" align="center" prop="projectYear" />
<!--      <el-table-column label="原主管部门" align="center" prop="departHead" />-->
<!--      <el-table-column label="市级" align="center" prop="city" />-->
<!--      <el-table-column label="县" align="center" prop="county" />-->
      <el-table-column label="乡镇" align="center" prop="town" />
      <el-table-column label="村名" align="center" prop="cun" />
      <el-table-column label="机井类型" align="center" prop="wellType" />
<!--      <el-table-column label="是否提升" align="center" prop="isPromote" />-->
      <el-table-column label="是否能用" align="center" prop="isUse" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="管护人" align="center" prop="ghyName" />
      <el-table-column label="联系方式" align="center" prop="ghyTel" />
<!--      <el-table-column label="审核通过时间" align="center" prop="approvalTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="维修人" align="center" prop="identifyName" />
      <el-table-column label="维修电话" align="center" prop="identifyPhone" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="createDv(scope.row)"
          >二维码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['farmwell:well:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['farmwell:well:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 二维码对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>

        <div style="text-align: center">
          <vue-qr
            ref="qrCode"
            :text="qrcodeText"
            :callback="qrCallBack"
            width="300"
            height="300"
          ></vue-qr>
        </div>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="downloadDv">下 载</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
      </el-col>
    </el-row>
    <!-- 添加或修改机井信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机井编号" prop="wellNumber">
          <el-input v-model="form.wellNumber" placeholder="请输入机井编号" />
        </el-form-item>
        <el-form-item label="原机井编号" prop="oldWellNumber">
          <el-input v-model="form.oldWellNumber" placeholder="请输入原机井编号" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
<!--        <el-form-item label="排查人" prop="identifyName">-->
<!--          <el-input v-model="form.identifyName" placeholder="请输入排查人" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="排查联系方式" prop="identifyPhone">-->
<!--          <el-input v-model="form.identifyPhone" placeholder="请输入排查联系方式" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审核通过时间" prop="approvalTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.approvalTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择审核通过时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="机井建设年份" prop="constructionYear">
          <el-input v-model="form.constructionYear" placeholder="请输入机井建设年份" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <div>-->
<!--          <vue-qr-->
<!--            ref="qrCode"-->
<!--            :text="form.wellNumber"-->
<!--            width="300"-->
<!--            height="300"-->
<!--          ></vue-qr>-->
<!--        </div>-->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWell, getWell,getQrText, delWell, addWell, updateWell,deptTreeSelect } from "@/api/farmwell/well";
import VueQr from 'vue-qr'

export default {
  name: "Well",
  components:{
    VueQr,
  },
  data() {
    return {
      qrcodeText:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 机井信息表格数据
      wellList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 二维码弹出层标题
      title1: "",
      // 下载数据base64
      qrDownloadUrl: '',
      // 是否显示二维码弹出层
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wellNumber: null,
        oldWellNumber: null,
        projectYear: null,
        departHead: null,
        city: null,
        county: null,
        town: null,
        wellType: null,
        isPromote: null,
        isUse: null,
        address: null,
        longitude: null,
        latitude: null,
        identifyName: null,
        identifyPhone: null,
        approvalTime: null,
        constructionYear: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    // 二维码刷新后调用
    qrCallBack(qrUrl) {
      this.qrDownloadUrl = qrUrl;
    },
    /** 查询机井信息列表 */
    getList() {
      this.loading = true;
      const urlParams = new URLSearchParams(window.location.search);
      // 从查询参数中获取名为 'param' 的值
      const type = urlParams.get('type');
      if(type=='town')
      {
        const town = urlParams.get('town');
        this.queryParams.town=town
      }
      if(type=='isUse')
      {
        const isUse = urlParams.get('isUse');
        this.queryParams.isUse=isUse
      }
      listWell(this.queryParams).then(response => {
        this.wellList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data, node) {
      this.resetForm("queryForm");
      // 获取父节点
      const parent = node.parent;
      // 根节点的parent是Vue实例，需要排除
      if (parent && parent.data) {
        this.parentNode = parent.data;
        if(parent.id != '1'&&parent.id != 1){
          this.queryParams.town = parent.label
          this.queryParams.cun = data.label;
        }else{
          this.queryParams.town = data.label
        }

      } else {
        this.parentNode = {label: '无父节点（根节点）', id: null};
      }
      // this.queryParams.town = data.label;
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wellNumber: null,
        oldWellNumber: null,
        projectYear: null,
        departHead: null,
        city: null,
        county: null,
        town: null,
        wellType: null,
        isPromote: null,
        isUse: null,
        address: null,
        longitude: null,
        latitude: null,
        identifyName: null,
        identifyPhone: null,
        approvalTime: null,
        constructionYear: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机井信息";
    },
    /** 修改按钮操作 */ async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      const code = row.wellNumber
      await getQrText(code).then(response => {
        this.qrcodeText = response.data;
      });
      await getWell(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改机井信息";
      });
    },
    /** 修改按钮操作 */ async createDv(row) {
      this.reset();
      const id = row.id
      const code = row.wellNumber
      await getQrText(code).then(response => {
        this.qrcodeText = response.data;
        console.log(id)
        this.qrcodeText = "http://111.6.105.59:8111/faultReport?param="+id;
      });
      await getWell(id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title1 = "机井二维码";
      });
    },
    /** 下载按钮操作 */ async downloadDv(row) {
      let a = document.createElement('a');
      a.download = this.form.wellNumber;
      a.href = this.qrDownloadUrl;
      a.click();
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWell(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWell(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除机井信息编号为"' + ids + '"的数据项？').then(function() {
        return delWell(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('farmwell/well/export', {
        ...this.queryParams
      }, `well_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
